#!/bin/bash


#################################################
#
#  @author： man deep as the sea
#  @date  :  2021-04-21
#  @desc  :  事件间隔分析任务
#  @contact: 13813838383388
#
#################################################

export JAVA_HOME=/opt/apps/jdk-11.0.9/
export HIVE_HOME=/opt/apps/hive-3.1.2/
export HADOOP_HOME=/opt/apps/hadoop-3.1.1/ 

dt=`date -d'-1 day' +%Y-%m-%d`

if [ $1 ]
then 
  dt=$1
fi  

${HIVE_HOME}/bin/hive -e "
set hive.vectorized.execution.enabled=false;
with tmp as (
select
  guid,
  \`timestamp\` as t1,
  eventid,
  lead(eventid,1,null) over(partition by guid order by \`timestamp\`) as next_event,
  lead(\`timestamp\`,1,null) over(partition by guid order by \`timestamp\`) as t2
from doit20dwd.app_event_detail
where dt='${dt}'
and ((eventid='pageView' and properties['pageId']='5')  or (eventid='search'))
)

insert into table doit20dws.app_event_interval partition(dt='${dt}')
select
guid,
eventid as event1,
next_event as event2,
t2-t1 as interval_time
from tmp
where next_event='pageView' and eventid='search'
"

$HIVE_EXIT_CODE=$?

if [ $HIVE_EXIT_CODE -eq 0 ] 
then
 echo "恭喜.... 任务执行成功"
else
 echo "节哀顺变.....挂了"
fi

exit $HIVE_EXIT_CODE






